/*
** pud_fork.c for pud in mrcellophane/libpud
** 
** Made by solomon hykes
** Login   <torgau@gmail.com>
** 
** Started on  Fri May 20 17:33:16 2005 solomon hykes
** Last update Fri May 20 17:41:20 2005 solomon hykes
*/

#include "pud_sys.h"

pid_t		pud_fork(pud_process *p)
{
  sys_pud_process	*proc = (sys_pud_process *)p;
  unsigned char		code[2] = { 0xcd, 0x80 };
  unsigned char		save[2];
  u_long		orig_pc;
  pid_t			result;

  pud_mem_read(p, proc->pc, save, 2);
  pud_mem_write(p, proc->pc, code, 2);
  proc->regs.r_eax = 0x2;
  sys_pud_setregs(proc);
  orig_pc = proc->pc;
  do
    {
      pud_step(p, 0);
      pud_next_event(p);
    }
  while (proc->pc <= orig_pc);
  result = proc->regs.r_eax;
  pud_mem_write(p, proc->pc, save, 2);
  return (result);
}
